﻿using System;
using System.Web;


/// <summary>
/// JsHelper 的摘要说明。
/// </summary>
public class JsHelper
{
    #region Alert
    /// <summary>
    /// 弹出警告对话框
    /// </summary>
    /// <param name="s"></param>
    public static void Alert(string s)
    {
        s = s.Replace("\"", "‘");
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>alert(\"", s, "\")</script>"));
    }
    #endregion

    #region Alert_Back
    /// <summary>
    /// 弹出警告对话框,并返回到原来的页面
    /// </summary>
    /// <param name="s"></param>
    public static void Alert_Back(string s)
    {
        s = s.Replace("\"", "‘");
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>alert(\"", s, "\");window.history.go(-1);</script>"));
        HttpContext.Current.Response.End();
    }
    #endregion

    #region Alert_Close
    /// <summary>
    /// 弹出警告对话框,并关闭当前窗口
    /// </summary>
    /// <param name="s"></param>
    public static void Alert_Close(string s)
    {
        s = s.Replace("\"", "‘");
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>alert(\"", s, "\");window.close();</script>"));
    }
    #endregion

    #region Alert_Close_Open
    /// <summary>
    /// 弹出警告对话框,并关闭当前窗口，并打开新窗口
    /// </summary>
    /// <param name="s"></param>
    /// <param name="newWindowUrl"></param>
    public static void Alert_Close_Open(string s, string newWindowUrl)
    {
        s = s.Replace("\"", "‘");
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>alert(\"", s, "\");window.close();window.open('" + newWindowUrl + "');</script>"));
    }
    #endregion

    #region Alert_Redirect
    /// <summary>
    /// 弹出警告对话框,并跳转到指定的地址
    /// </summary>
    /// <param name="s"></param>
    /// <param name="targetUrl"></param>
    public static void Alert_Redirect(string s, string targetUrl)
    {
        s = s.Replace("\"", "‘");
        HttpContext.Current.Response.Write(String.Concat(new string[] { "<script language=Javascript>alert(\"", s, "\");window.location.href=\"", targetUrl, "\";</script>" }));
        HttpContext.Current.Response.End();
    }
    #endregion

    

    public static void RtnRltMsgbox(object message, string strWinCtrl)
    {
        string js = "<Script language='JavaScript'>\r\n";
        js += strWinCtrl + " = true;\r\n";
        js += " if(!confirm('" + message + "'))" + strWinCtrl + "=false;\r\n</Script>\r\n";
        HttpContext.Current.Response.Write(string.Format(js, message.ToString()));
    }

    /// <summary>
    /// 回到历史页面
    /// </summary>
    /// <param name="value">-1/1</param>
    public static void GoHistory(int value)
    {
        string js = @"<Script language='JavaScript'>
                    history.go({0});  
                  </Script>";
        HttpContext.Current.Response.Write(string.Format(js, value));
    }

    /// <summary>
    /// 关闭当前窗口
    /// </summary>
    public static void CloseWindow()
    {
        string js = @"<Script language='JavaScript'>
                    window.close();  
                  </Script>";
        HttpContext.Current.Response.Write(js);
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 刷新父窗口
    /// </summary>
    public static void RefreshParent()
    {
        string js = @"<Script language='JavaScript'>
                    parent.location.reload();
                  </Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    /// 格式化为JS可解释的字符串
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string JSStringFormat(string s)
    {
        return s.Replace("\r", "\\r").Replace("\n", "\\n").Replace("'", "\\'").Replace("\"", "\\\"");
    }

    /// <summary>
    /// 刷新打开窗口
    /// </summary>
    public static void RefreshOpener()
    {
        string js = @"<Script language='JavaScript'>
                    opener.location.reload();
                  </Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    /// 打开窗口
    /// </summary>
    /// <param name="url"></param>
    public static void OpenWebForm(string url)
    {

  
        string js = @"<Script language='JavaScript'>
			//window.open('" + url + @"');
			window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
			</Script>";
        /*结束*/
        /*…………………………………………………………………………………………*/


        HttpContext.Current.Response.Write(js);
    }
    public static void OpenWebForm(string url, string name, string future)
    {
        string js = @"<Script language='JavaScript'>
                     window.open('" + url + @"','" + name + @"','" + future + @"')
                  </Script>";
        HttpContext.Current.Response.Write(js);
    }
    public static void OpenWebForm(string url, string formName)
    {
 
        string js = @"<Script language='JavaScript'>
			//window.open('" + url + @"','" + formName + @"');
			window.open('" + url + @"','" + formName + @"','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
			</Script>";
        /*结束*/
        /*…………………………………………………………………………………………*/

        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:OpenWebForm	
    /// 功能描述:打开WEB窗口	
    /// </summary>
    /// <param name="url">WEB窗口</param>
    /// <param name="isFullScreen">是否全屏幕</param>
    public static void OpenWebForm(string url, bool isFullScreen)
    {
        string js = @"<Script language='JavaScript'>";
        if (isFullScreen)
        {
            js += "var iWidth = 0;";
            js += "var iHeight = 0;";
            js += "iWidth=window.screen.availWidth-10;";
            js += "iHeight=window.screen.availHeight-50;";
            js += "var szFeatures ='width=' + iWidth + ',height=' + iHeight + ',top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no';";
            js += "window.open('" + url + @"','',szFeatures);";
        }
        else
        {
            js += "window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');";
        }
        js += "</Script>";
        HttpContext.Current.Response.Write(js);
    }
    /// <summary>
    /// 转向Url制定的页面
    /// </summary>
    /// <param name="url"></param>
    public static void JavaScriptLocationHref(string url)
    {
        string js = @"<Script language='JavaScript'>
                    window.location.replace('{0}');
                  </Script>";
        js = string.Format(js, url);
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    /// 指定的框架页面转换
    /// </summary>
    /// <param name="FrameName"></param>
    /// <param name="url"></param>
    public static void JavaScriptFrameHref(string FrameName, string url)
    {
        string js = @"<Script language='JavaScript'>
					
                    @obj.location.replace(""{0}"");
                  </Script>";
        js = js.Replace("@obj", FrameName);
        js = string.Format(js, url);
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    ///重置页面
    /// </summary>
    public static void JavaScriptResetPage(string strRows)
    {
        string js = @"<Script language='JavaScript'>
                    window.parent.CenterFrame.rows='" + strRows + "';</Script>";
        HttpContext.Current.Response.Write(js);
    }
    /// <summary>
    /// 函数名:JavaScriptSetCookie
    /// 功能描述:客户端方法设置Cookie
    /// 作者:sxs
    /// 日期：2003-4-9
    /// 版本：1.0
    /// </summary>
    /// <param name="strName">Cookie名</param>
    /// <param name="strValue">Cookie值</param>
    public static void JavaScriptSetCookie(string strName, string strValue)
    {
        string js = @"<script language=Javascript>
			var the_cookie = '" + strName + "=" + strValue + @"'
			var dateexpire = 'Tuesday, 01-Dec-2020 12:00:00 GMT';
			//document.cookie = the_cookie;//写入Cookie<BR>} <BR>
			document.cookie = the_cookie + '; expires='+dateexpire;			
			</script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:GotoParentWindow	
    /// 功能描述:返回父窗口	
    /// </summary>
    /// <param name="parentWindowUrl">父窗口</param>		
    public static void GotoParentWindow(string parentWindowUrl)
    {
        string js = @"<Script language='JavaScript'>
                    this.parent.location.replace('" + parentWindowUrl + "');</Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:ReplaceParentWindow	
    /// 功能描述:替换父窗口	
    /// </summary>
    /// <param name="parentWindowUrl">父窗口</param>
    /// <param name="caption">窗口提示</param>
    /// <param name="future">窗口特征参数</param>
    public static void ReplaceParentWindow(string parentWindowUrl, string caption, string future)
    {
        string js = "";
        if (future != null && future.Trim() != "")
        {
            js = @"<script language=javascript>this.parent.location.replace('" + parentWindowUrl + "','" + caption + "','" + future + "');</script>";
        }
        else
        {
            js = @"<script language=javascript>var iWidth = 0 ;var iHeight = 0 ;iWidth=window.screen.availWidth-10;iHeight=window.screen.availHeight-50;
							var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';this.parent.location.replace('" + parentWindowUrl + "','" + caption + "',szFeatures);</script>";
        }

        HttpContext.Current.Response.Write(js);
    }


    /// <summary>		
    /// 函数名:ReplaceOpenerWindow	
    /// 功能描述:替换当前窗体的打开窗口	
    /// </summary>
    /// <param name="openerWindowUrl">当前窗体的打开窗口</param>		
    public static void ReplaceOpenerWindow(string openerWindowUrl)
    {
        string js = @"<Script language='JavaScript'>
                    window.opener.location.replace('" + openerWindowUrl + "');</Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:ReplaceOpenerParentWindow	
    /// 功能描述:替换当前窗体的打开窗口的父窗口	
    /// </summary>
    /// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>		
    public static void ReplaceOpenerParentFrame(string frameName, string frameWindowUrl)
    {
        string js = @"<Script language='JavaScript'>
                    window.opener.parent." + frameName + ".location.replace('" + frameWindowUrl + "');</Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:ReplaceOpenerParentWindow	
    /// 功能描述:替换当前窗体的打开窗口的父窗口	
    /// </summary>
    /// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>		
    public static void ReplaceOpenerParentWindow(string openerParentWindowUrl)
    {
        string js = @"<Script language='JavaScript'>
                    window.opener.parent.location.replace('" + openerParentWindowUrl + "');</Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>		
    /// 函数名:CloseParentWindow	
    /// 功能描述:关闭窗口	
    /// </summary>
    public static void CloseParentWindow()
    {
        string js = @"<Script language='JavaScript'>
                    window.parent.close();  
                  </Script>";
        HttpContext.Current.Response.Write(js);
    }

    public static void CloseOpenerWindow()
    {
        string js = @"<Script language='JavaScript'>
                    window.opener.close();  
                  </Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    /// 函数名:ShowModalDialogJavascript	
    /// 功能描述:返回打开模式窗口的脚本	
    /// </summary>
    /// <param name="webFormUrl"></param>
    /// <returns></returns>
    public static string ShowModalDialogJavascript(string webFormUrl)
    {
        string js = @"<script language=javascript>
							var iWidth = 0 ;
							var iHeight = 0 ;
							iWidth=window.screen.availWidth-10;
							iHeight=window.screen.availHeight-50;
							var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';
							showModalDialog('" + webFormUrl + "','',szFeatures);</script>";
        return js;
    }

    public static string ShowModalDialogJavascript(string webFormUrl, string features)
    {
        string js = @"<script language=javascript>							
							showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
        return js;
    }

    /// <summary>
    /// 函数名:ShowModalDialogWindow	
    /// 功能描述:打开模式窗口	

    /// </summary>
    /// <param name="webFormUrl"></param>
    /// <returns></returns>
    public static void ShowModalDialogWindow(string webFormUrl)
    {
        string js = ShowModalDialogJavascript(webFormUrl);
        HttpContext.Current.Response.Write(js);
    }

    public static void ShowModalDialogWindow(string webFormUrl, string features)
    {
        string js = ShowModalDialogJavascript(webFormUrl, features);
        HttpContext.Current.Response.Write(js);
    }
    public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
    {
        string features = "dialogWidth:" + width.ToString() + "px"
            + ";dialogHeight:" + height.ToString() + "px"
            + ";dialogLeft:" + left.ToString() + "px"
            + ";dialogTop:" + top.ToString() + "px"
            + ";center:yes;help=no;resizable:no;status:no;scroll=no";
        ShowModalDialogWindow(webFormUrl, features);
    }

    public static void SetHtmlElementValue(string formName, string elementName, string elementValue)
    {
        string js = @"<Script language='JavaScript'>if(document." + formName + "." + elementName + "!=null){document." + formName + "." + elementName + ".value =" + elementValue + ";}</Script>";
        HttpContext.Current.Response.Write(js);
    }

    /// <summary>
    /// 关闭浮动半透明层
    /// </summary>
    public static void 关闭浮动半透明层()
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.resetWindow()</script>"));
    }

    /// <summary>
    /// 关闭浮动半透明层或者后退
    /// </summary>
    /// <param name="closeButton"></param>
    public static void 关闭层OR后退(string closeButton)
    {
        if (closeButton == "Close")
        {
            关闭浮动半透明层();
        }
        if (closeButton == "Return")
        {
            GoHistory(-1);
        }

    }
    /// <summary>
    /// 关闭浮动半透明层并刷新父页面
    /// </summary>
    public static void 关闭浮动半透明层并刷新父页面()
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.location.reload();parent.resetWindow()</script>"));
    }
    /// <summary>
    /// 关闭浮动半透明层并刷新父页面的UpdatePanel
    /// </summary>
    public static void 关闭浮动半透明层并刷新父页面的UpdatePanel()
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.__doPostBack('UpdatePanel1', 'refreshUpdatePanel');parent.resetWindow();</script>"));
    }
    /// <summary>
    /// 关闭浮动半透明层并刷新父页面的UpdatePanel，局部更新repeat中的某一条
    /// </summary>
    /// <param name="rp_item_i_">要更新的repeat的item的keyid</param>
    public static void 关闭浮动半透明层并刷新父页面的UpdatePanel的Repeat(int rp_item_keyid_)
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.__doPostBack('UpdatePanel1', 'refresh_Repeat," + rp_item_keyid_ + "');parent.resetWindow();</script>"));
    }
    public static void 跳转到新页面_或者_关闭浮动半透明层并刷新父页面的UpdatePanel的Repeat(string tiaojian,string newhref,int rp_item_keyid_)
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>if(" + tiaojian + "){parent.location.href='" + newhref + "'}else{parent.__doPostBack('UpdatePanel1', 'refresh_Repeat," + rp_item_keyid_ + "');parent.resetWindow();}</script>"));
    }
    public static void js(string jsStr)
    {
        string js = @"<Script language='JavaScript'>" + jsStr + "</Script>";
        HttpContext.Current.Response.Write(js);
    }
    /// <summary>
    /// 实时修改GD里的订单列表
    /// </summary>
    /// <param name="editType_">修改方式  insert,update,edit,OK</param>
    /// <param name="json_">json格式数据</param>
    public static void editGDOrders(string editType_, string json_)
    {
        string sc = "parent.editGDOrders('" + editType_ + "'," + json_ + ");parent.reCoundMenu();";
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>"+sc+"</script>"));
    }
    public static void close_adminRight()
    {
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.close_adminRight();</script>"));
    }

    /// <summary>
    /// 刷新updatepanel列表数据
    /// </summary>
    public static void refresh_Page_Bind()
    {
        string sc = "parent.refresh_Page_Bind();";
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>" + sc + "</script>"));
    }
    /// <summary>
    /// 浮动层提示框
    /// </summary>
    /// <param name="msgtitle">提示文字</param>
    /// <param name="isSuccess">是否成功</param>
    public static void Alert_float(string msgtitle, bool isSuccess)
    {
        string msgcss = "correct";
        if (!isSuccess) msgcss = "error";
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>parent.Alert_float_(\"" + msgtitle + "\",\"" + msgcss + "\")</script>"));
    }
    /// <summary>
    /// 刷新 frame_list
    /// </summary>
    public static void frame_list_page_build()
    {
        frame_list_page_build("");
    }
    /// <summary>
    /// 刷新 frame_list
    /// </summary>
    /// <param name="fenghuolun_TrDatakey">风火轮要盖住的tr的key，为""则风火轮笼罩整个tablebody，为"null"不出现风火轮</param>
    public static void frame_list_page_build(string fenghuolun_TrDatakey)
    {
        string sc = "parent.frame_list_page_build(\"" + fenghuolun_TrDatakey + "\");";
        HttpContext.Current.Response.Write(String.Concat("<script language=Javascript>" + sc + "</script>"));
    }
}

